<template>
  <div class="login-container">
    <el-card class="login-form">
      <div class="login-title">Gate IO</div>
      <el-form
          ref="loginFormRef"
          :model="loginForm"
          :rules="rules"
          class="demo-ruleForm"
      >
        <el-form-item label="手机/邮箱" prop="username" label-width="100px">
          <el-input v-model="loginForm.username" placeholder="请输入手机或邮箱" />
        </el-form-item>
        <el-form-item label="密码" prop="password" label-width="100px">
          <el-input
              v-model="loginForm.password"
              type="password"
              placeholder="请输入密码"
          />
        </el-form-item>
        <el-form-item label="确认密码" prop="password2" label-width="100px">
          <el-input
              v-model="loginForm.password2"
              type="password"
              placeholder="请确认密码"
          />
        </el-form-item>
        <el-form-item label="验证码" prop="captcha" label-width="100px">
          <el-input
              v-model="loginForm.captcha"
              placeholder="请输入验证码"
              autocomplete="off"
          />
          <captcha-component ref="captchaRef" @getValue="getValue" style="height: 40px; width: 90px; margin-left: 10px;"></captcha-component>
        </el-form-item>
        <el-form-item>
          <el-button type="success" @click="submitForm" style="width: 50%;margin-top: 10px;margin-left: 90px">注册</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { reactive, ref } from 'vue';
import CaptchaComponent from "@/components/utils/Verification.vue";
import {ElMessage} from "element-plus";
import {useRouter} from "vue-router";

const loginFormRef = ref(null);
const router = useRouter();
const loginForm = reactive({
  username: '',
  password: '',
  password2:'',
  captcha: ''
});
const submitSuccess = () => {
  ElMessage.success('登录成功');
};
const validateUsernameOrEmail = (rule, value, callback) => {
  const phoneRegex = /^1[3-9]\d{9}$/;
  const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;

  if (phoneRegex.test(value) || emailRegex.test(value)) {
    callback();
  } else {
    callback(new Error('请输入有效的手机或邮箱'));
  }
};
const validatePasswordConsistency = (rule, value, callback) => {
  if (value !== loginForm.password) {
    callback(new Error('两次输入的密码不一致'));
  } else {
    callback();
  }
};
const rules = reactive({
  username: [
    { validator: validateUsernameOrEmail, trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能小于6位', trigger: 'blur' }
  ],
  password2: [
    { required: true, message: '请再次输入密码', trigger: 'blur' },
    { validator: validatePasswordConsistency, trigger: 'blur' }
  ],
  captcha: [
    { required: true, message: '请输入验证码', trigger: 'blur' }
  ]
});



const captchaRef = ref(null);

const getValue = (value) => {
  loginForm.captcha = value;
};

const submitForm = () => {
  if (!loginFormRef.value) return;
  loginFormRef.value.validate((valid) => {
    if (valid) {
      console.log('注册信息：', loginForm);
      submitSuccess();
      router.push('/login');

    } else {
      console.log('error submit!');
      return false;
    }
  });
};
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f5f5f5f5;
}

.login-form {
  width: 350px;
  padding: 30px;
  background-color: white;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  border-radius: 8px;
}

.login-title {
  text-align: center;
  font-size: 24px;
  margin-bottom: 20px;
}

.register-link {
  color: #409EFF;
  text-decoration: none;
}

.register-link:hover {
  text-decoration: underline;
}
</style>